<template>
  <div class="post-detail-page">
    <modal
      title="删除文章"
      :visible="modalIsVisible"
      @modal-on-close="modalIsVisible = false"
      @modal-on-confirm="hideAndDelete"
    >
      <p>确定要删除这篇文章吗？</p>
    </modal>
    <article class="w-75 mx-auto mb-5 pb-3" v-if="currentPost">
      <img
        :src="currentImageUrl"
        alt="currentPost.title"
        class="rounded-lg img-fluid my-4"
        v-if="currentImageUrl"
      />
      <h2 class="mb-4">{{ currentPost.title }}</h2>
      <div
        class="user-profile-component border-top border-bottom py-3 mb-5 align-items-center row g-0"
      >
        <div class="col">
          <user-profile
            :user="currentPost.author"
            v-if="typeof currentPost.author === 'object'"
          ></user-profile>
        </div>
        <span class="text-muted col text-right font-italic"
          >发表于：{{ currentPost.createdAt }}</span
        >
      </div>
      <div v-html="currentHTML"></div>
      <div v-if="showEditArea" class="btn-group mt-5">
        <router-link
          type="button"
          class="btn btn-success"
          :to="{ name: 'create', query: { id: currentPost._id } }"
        >
          编辑
        </router-link>
        <button type="button" class="btn btn-danger" @click.prevent="modalIsVisible = true">
          删除
        </button>
      </div>
    </article>
  </div>
</template>

<script lang="ts">
import { defineComponent, onMounted, computed, ref } from 'vue'
import { marked } from 'marked'
import { useRoute, useRouter } from 'vue-router'
import { usePostStore } from '@/store/post' // pinia
import { useUserStore, UserDataProps } from '@/store/user' // ts
import { ImageProps } from '@/store/utils' // ts
import UserProfile from '@/components/UserProfile.vue'
import Modal from '@/components/Modal.vue'
import createMessage from '@/components/createMessage' // 创建消息弹窗

export default defineComponent({
  name: 'post-detail',
  components: {
    UserProfile,
    Modal
  },
  setup() {
    const route = useRoute() // 当前路由信息
    const router = useRouter() // 路由实例
    const postStore = usePostStore() // 专栏信息
    const userStore = useUserStore() // 用户信息
    const modalIsVisible = ref(false) // 弹窗显示隐藏
    const currentId = route.params.id as string // 专栏ID
    onMounted(() => {
      postStore.fetchPost(currentId)
    })
    // 获取专栏信息
    const currentPost = computed(() => postStore.getCurrentPost(currentId))
    // 判断专栏内容是字符串还是html内容
    const currentHTML = computed(() => {
      if (currentPost.value && currentPost.value.content) {
        const { isHTML, content } = currentPost.value
        return isHTML ? content : marked.parse(content)
      } else {
        return ''
      }
    })
    // 判断是否是自己的专栏，决定是否显示编辑或者删除
    const showEditArea = computed(() => {
      // 用户是否存在、是否登录
      if (currentPost.value && currentPost.value.author && userStore.isLogin) {
        const postAuthor = currentPost.value.author as UserDataProps
        return postAuthor._id === userStore.data?._id
      } else {
        return false
      }
    })
    const currentImageUrl = computed(() => {
      if (currentPost.value && currentPost.value.image) {
        const { image } = currentPost.value
        return (image as ImageProps).url + '?x-oss-process=image/resize,w_850'
      } else {
        return null
      }
    })
    // 关闭弹窗
    const hideAndDelete = () => {
      modalIsVisible.value = false
      // 删除 API
      postStore.deletePost(currentId).then(data => {
        createMessage('删除成功，2秒后跳转到专栏首页', 'success', 2000)
        setTimeout(() => {
          router.push({ name: 'column', params: { id: data.column } })
        }, 2000)
      })
    }
    return {
      currentPost,
      currentImageUrl,
      currentHTML,
      showEditArea,
      modalIsVisible,
      hideAndDelete
    }
  }
})
</script>
